'''
Created on 01/07/2010

@author: sbrito
'''

from xml.etree import ElementTree
from lib.BeautifulSoup import BeautifulSoup
from lib.utils import prettify
import re

def scrap_candidate(content):
    if content == None:
        return None
    
    try:
        soup = BeautifulSoup(content)
        
        content_table = soup.html.body.form.findAll('table', recursive=False)[2].tr.findAll('td', recursive=False)[1].table
        content_table_lines = content_table.findAll('tr', recursive=False)
        
        content_table_1st_line = content_table_lines[0].findAll('td')
        content_table_2nd_line = content_table_lines[1].findAll('td')
        content_table_3rd_line = content_table_lines[2].findAll('td')
        content_table_4th_line = content_table_lines[3].findAll('td')
        content_table_5th_line = content_table_lines[4].findAll('td')
        content_table_6th_line = content_table_lines[5].findAll('td')
        content_table_7th_line = content_table_lines[7].findAll('td')
        content_table_8th_line = content_table_lines[8].findAll('td')
        content_table_9th_line = content_table_lines[9].findAll('td')
        content_table_10th_line = content_table_lines[10].findAll('td')
        content_table_11th_line = content_table_lines[11].findAll('td')
        content_table_12th_line = content_table_lines[12].findAll('td')
        content_table_13th_line = content_table_lines[13].findAll('td')
        content_table_14th_line = content_table_lines[14].findAll('td')
        
        goods_table_lines = soup.find('div', id='div_bens').table.findAll('tr', attrs = {'class': re.compile('(linhaPreenchida|linhaNaoPreenchida)')}, recursive=False)
        
        candidate = {}
        
        candidate['nome_urna'] = prettify(content_table_1st_line[1].contents[0])
        candidate['numero'] = prettify(content_table_1st_line[3].contents[0])
        
        candidate['nome_completo'] = prettify(content_table_2nd_line[1].contents[0])
        candidate['sexo'] = prettify(content_table_2nd_line[3].contents[0])
        
        candidate['data_nascimento'] = prettify(content_table_3rd_line[1].contents[0])
        candidate['estado_civil'] = prettify(content_table_3rd_line[3].contents[0])
        
        candidate['nacionalidade'] = prettify(content_table_4th_line[1].contents[0])
        candidate['naturalidade'] = prettify(content_table_4th_line[3].contents[0])
        
        candidate['instrucao'] = prettify(content_table_5th_line[1].contents[0])
        candidate['ocupacao'] = prettify(content_table_5th_line[3].contents[0])
        
        candidate['site'] = prettify(content_table_6th_line[1].findAll('a')[0].contents[0]) if len(content_table_6th_line[1].findAll('a')[0].contents) > 0 else ''
        
        candidate['partido'] = prettify(content_table_7th_line[1].contents[0])
        
        candidate['coligacao'] = prettify(content_table_8th_line[1].contents[0])
        
        candidate['composicao_coligacao'] = prettify(content_table_9th_line[1].contents[0])
        
        candidate['cargo_que_concorre'] = prettify(content_table_10th_line[1].contents[0])
        
        candidate['processo_protocolo'] = prettify(content_table_11th_line[1].contents[0])
        
        candidate['resultado_eleicao'] = prettify(content_table_12th_line[1].contents[0])
        
        candidate['julgamento_contas'] = prettify(content_table_13th_line[1].contents[0])
        
        candidate['cnpj_campanha'] = prettify(content_table_14th_line[1].contents[0])
        
        candidate['bens'] = []
        
        for line in goods_table_lines:
            desc = prettify(line.findAll('td')[1].contents[0])
            good_type = prettify(line.findAll('td')[2].contents[0])
            value = prettify(line.findAll('td')[3].contents[0])
            
            candidate['bens'].append({'desc': desc, 'type': good_type, 'value': value})
      
    #    print candidate['bens']
    #    
    #    print candidate['nome_urna']
    #    print candidate['numero']
    #    print candidate['nome_completo']
    #    print candidate['sexo']
    #    print candidate['data_nascimento']
    #    print candidate['estado_civil']
    #    print candidate['nacionalidade']
    #    print candidate['naturalidade']
    #    print candidate['instrucao']
    #    print candidate['ocupacao']
    #    print candidate['partido']
    #    print candidate['coligacao']
    #    print candidate['composicao_coligacao']
    #    print candidate['cargo_que_concorre']
    #    print candidate['processo_protocolo']
    #    print candidate['resultado_eleicao']
    #    print candidate['julgamento_contas']
    #    print candidate['cnpj_campanha']
            
        return candidate
    except:
        return None

def scrap_candidate_list(content):
    response = ElementTree.fromstring(content)
    
    return filter(lambda p: p != '.', [e.text for e in response.findall('sqCand')])